package com.globallogic.bean;

import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import com.globallogic.consts.AppPages;
import com.globallogic.model.Activity;
import com.globallogic.model.entity.DailyPlan;
import com.globallogic.model.service.DailyPlanService;
import com.globallogic.model.transferobject.DailyPlanTo;

@ManagedBean
@SessionScoped
public class DailyPlanBean {

	public Activity[] getActivities() {
		return Activity.values();
	}

	public String create() {
		dailyPlanTo.setDefValues();
		dailyPlanTo.username = loginBean.getName();

		return AppPages.dailyPlan + "?faces-redirect=true";
	}

	public String update(int id) {
		dailyPlanTo = dailyPlanService.getDailyPlanById(id);
		isUpdate = true;

		return AppPages.dailyPlan + "?faces-redirect=true";
	}

	public String cancel() {
		return AppPages.dailyPlanList + "?faces-redirect=true";
	}

	public String saveChanges() {

		boolean result;

		if (isUpdate) {
			result = dailyPlanService.updateDailyPlan(dailyPlanTo.getEntity());
		} else {
			result = dailyPlanService.createDailyPlan(dailyPlanTo.getEntity());
		}

		if (result) {
			return AppPages.dailyPlanList + "?faces-redirect=true";
		} else {
			return "";
		}

	}

	public void remove(int id) {
		dailyPlanService.deleteDailyPlan(id);
	}

	public void update() {
		dailyPlanService.updateDailyPlan(dailyPlanTo.getEntity());
	}

	public List<DailyPlan> getPlans() {
		return dailyPlanService.getDailyPlans();
	}

	public Date getDate() {
		return dailyPlanTo.date;
	}

	public void setDate(Date date) {
		dailyPlanTo.date = date;
	}

	public Activity getActivity() {
		return dailyPlanTo.activity;
	}

	public void setActivity(Activity activity) {
		dailyPlanTo.activity = activity;
	}

	public int getEstimateDuration() {
		return dailyPlanTo.estimateDuration;
	}

	public void setEstimateDuration(int estimateDuration) {
		dailyPlanTo.estimateDuration = estimateDuration;
	}

	public int getActualDuration() {
		return dailyPlanTo.actualDuration;
	}

	public void setActualDuration(int actualDuration) {
		dailyPlanTo.actualDuration = actualDuration;
	}

	public String getComments() {
		return dailyPlanTo.comments;
	}

	public void setComments(String comments) {
		dailyPlanTo.comments = comments;
	}

	public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
	}

	@EJB
	private DailyPlanService dailyPlanService;
	private DailyPlanTo dailyPlanTo = new DailyPlanTo();

	private boolean isUpdate = false;

	@ManagedProperty(value = "#{loginBean}")
	LoginBean loginBean;
}
